package com.yc.jpyy.usbkey.service.imp;

import com.yc.jpyy.usbkey.dao.UsbkeyDao;
import com.yc.jpyy.usbkey.service.UsbkeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by thinkpad on 2018/8/11.
 */
@Service
public class UsbkeyServiceImpl implements UsbkeyService {

    @Autowired
    private UsbkeyDao usbkeyDao;

    @Override
    //根据userId和mac地址返回用户公钥信息
    public Map<String, Object> queryUsbkeyInfo(Map<String, Object> paramsMap) throws DataAccessException, ParseException {
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("userName", paramsMap.get("userName").toString());
        Map<String,Object> usbkeyInfo = usbkeyDao.queryUsbkeyInfo(paramsMap);
        if(usbkeyInfo != null && usbkeyInfo.size() > 0){
            //判断是否过期
            String enddate = usbkeyInfo.get("EndTime").toString();
            if(enddate != null && enddate.length() != 0){
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date dbdate = format.parse(enddate);
                Date curdate = new Date();
                if (curdate.getTime() > dbdate.getTime()) {
                    resultMap.put("returnCode", 0);
                    resultMap.put("returnMsg", "证书过期");
                }else{
                    // 判断MAC是否正确
                    String dbmac = usbkeyInfo.get("MacAddress").toString();
                    String curmac = paramsMap.get("mac").toString();
                    if(dbmac.isEmpty() || !dbmac.contains(curmac)){
                    	if(dbmac.split(",").length == 1) {
                    		 resultMap.put("returnCode", 0);
                             resultMap.put("returnMsg", "mac地址不一致");
                    	}else if(dbmac.split(",").length == 2) {
                   		    resultMap.put("returnCode", 0);
                            resultMap.put("returnMsg", "非法的客户端");
                     	}else if(dbmac.split(",").length == 0) {
                   		    resultMap.put("returnCode", 0);
                            resultMap.put("returnMsg", "非法的客户端");
                     	}
                    }else{
                        // 返回公钥数据
                        String dbpubkey = usbkeyInfo.get("PublicKey").toString();
                        if(dbpubkey != null && dbpubkey.length() != 0){
                            resultMap.put("returnCode", 1);
                            resultMap.put("ListData", dbpubkey);
                            resultMap.put("returnMsg", "操作成功");
                        }else{
                            resultMap.put("returnCode", 0);
                            resultMap.put("returnMsg", "证书暂未获取到");
                        }
                    }
                }
            }else{
                resultMap.put("returnCode", 0);
                resultMap.put("returnMsg", "过期时间为空");
            }
        }else{
            resultMap.put("returnCode", 0);
            resultMap.put("returnMsg", "系统无数据");
        }
        return resultMap;
    }
}
